package com.yugao.fintech.vctgone.log.listener;

import com.yugao.fintech.vctgone.log.event.LogEvent;
import lombok.RequiredArgsConstructor;
import com.yugao.fintech.vctgone.auth.api.dto.LogDTO;
import com.yugao.fintech.vctgone.auth.api.feign.FeignLogService;
import com.yugao.fintech.vctgone.feign.util.AccessTokenHolder;
import org.springframework.context.event.EventListener;
import org.springframework.scheduling.annotation.Async;

@RequiredArgsConstructor
public class LogListener {
    private final FeignLogService feignLogService;

    @Async
    @EventListener(LogEvent.class)
    public void processLogEvent(LogEvent event) {
        try {
            LogDTO logDTO = (LogDTO) event.getSource();
            String token = event.getToken();
            AccessTokenHolder.setToken(token);
            feignLogService.saveLog(logDTO);
        } catch (Exception e) {
            AccessTokenHolder.remove();
            throw new RuntimeException(e);
        }
    }
}
